/*
    SUSAN® - Sole of Unix Save ANything

   
      Copyright (C) 2011-2016 Skyatlas Co. LTD

   

   

   

   
*/
#if defined(HAVE_MINGW)
#include "include/susan.h"
#include "gtest/gtest.h"
#else
#include "gtest/gtest.h"
#include "include/susan.h"
#endif

#include "lib/parse_conf.h"
#include "dird/dird_globals.h"
#include "dird/dird_conf.h"
#include "dird/ua_cmds.h"

using namespace std;
using namespace directordaemon;

namespace directordaemon {
bool DoReloadConfig() { return false; }
}  // namespace directordaemon

class SetdebugTest : public ::testing::Test {
 protected:
  static void SetUpTestCase();
};

static std::multiset<std::string> client_names;
static std::multiset<std::string> stored_names;

static void DoClientSetdebug(UaContext* ua,
                             ClientResource* client,
                             int level,
                             int trace_flag,
                             int hangup_flag,
                             int timestamp_flag)
{
  client_names.insert(client->resource_name_);
}

static void DoStorageSetdebug(UaContext* ua,
                              StorageResource* store,
                              int level,
                              int trace_flag,
                              int timestamp_flag)
{
  stored_names.insert(store->resource_name_);
}

void SetdebugTest::SetUpTestCase()
{
  OSDependentInit();
  std::string path_to_config_file = std::string(
      RELATIVE_PROJECT_SOURCE_DIR "/configs/susan-configparser-tests");
  my_config = InitDirConfig(path_to_config_file.c_str(), M_ERROR_TERM);
  ASSERT_TRUE(my_config);
  ASSERT_TRUE(my_config->ParseConfig());

  SetDoClientSetdebugFunction(DoClientSetdebug);
  SetDoStorageSetdebugFunction(DoStorageSetdebug);

  DoAllSetDebug(nullptr, 0, 0, 0, 0);

  if (my_config) { delete my_config; }
}

TEST_F(SetdebugTest, test_clients_list)
{
  ASSERT_EQ(client_names.size(), 3);
  EXPECT_EQ(client_names.count("susan-fd"), 1);
  EXPECT_EQ(client_names.count("susan-fd2"), 1);
  EXPECT_EQ(client_names.count("susan-fd3"), 1);
  EXPECT_EQ(client_names.count("susan-fd-duplicate-interface"), 0);
}

TEST_F(SetdebugTest, test_storages_list)
{
  ASSERT_EQ(stored_names.size(), 3);
  EXPECT_EQ(stored_names.count("File"), 1);
  EXPECT_EQ(stored_names.count("File2"), 1);
  EXPECT_EQ(stored_names.count("File3"), 1);
  EXPECT_EQ(stored_names.count("File-duplicate-interface"), 0);
}
